package com.central.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.central.modules.sys.dao.SysDictTypeDao;
import com.central.modules.sys.entity.SysDictTypeEntity;
import com.central.modules.sys.model.AddSysDictTypeRequest;
import com.central.modules.sys.model.SysDictTypeQueryBean;
import com.central.modules.sys.service.SysDictTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeDao, SysDictTypeEntity> implements SysDictTypeService {
    @Override
    public IPage<SysDictTypeEntity> searchSysDictTypes(SysDictTypeQueryBean queryBean) {
        Page<SysDictTypeEntity> page = new Page<>(queryBean.getPageStart(), queryBean.getPageSize());
        LambdaQueryWrapper<SysDictTypeEntity> queryWrapper = new LambdaQueryWrapper<SysDictTypeEntity>();
        queryWrapper.orderByDesc(SysDictTypeEntity::getCreateDate);
        if (StringUtils.isNotBlank(queryBean.getDictType())) {
            queryWrapper.eq(SysDictTypeEntity::getDictType, queryBean.getDictType());
        }
        return this.page(page, queryWrapper);
    }

    @Override
    public boolean addSysDictType(AddSysDictTypeRequest request) {
        SysDictTypeEntity sysDictType = new SysDictTypeEntity();
        BeanUtils.copyProperties(request, sysDictType);
        sysDictType.setStatus(1);
        this.save(sysDictType);
        return true;
    }

    @Override
    public boolean updateSysDictType(Long id, AddSysDictTypeRequest request) {
        SysDictTypeEntity sysDictType = new SysDictTypeEntity();
        sysDictType.setDictName(request.getDictName());
        sysDictType.setRemark(request.getRemark());
        sysDictType.setId(id);
        return this.updateById(sysDictType);
    }

    @Override
    public boolean removeSysDictType(Long id) {
        return this.removeById(id);
    }

    @Override
    public SysDictTypeEntity getSysDictTypeByType(String dictType) {
        LambdaQueryWrapper<SysDictTypeEntity> queryWrapper = new LambdaQueryWrapper<SysDictTypeEntity>();
        queryWrapper.eq(SysDictTypeEntity::getDictType, dictType);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    public List<SysDictTypeEntity> findAll() {
        LambdaQueryWrapper<SysDictTypeEntity> queryWrapper = new LambdaQueryWrapper<SysDictTypeEntity>();
        queryWrapper.eq(SysDictTypeEntity::getStatus, 1);
        return this.list(queryWrapper);
    }
}
